﻿Imports System.IO
Public Class FormEducationAED
    Public eduId, empId As Int16
    Dim emp As New EmployeeInfo
    Public edu As New EducationInfo
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        MyClear.ClearAllTeax(EmployeeGB)

        LoadName()
    End Sub

    Private Sub LoadName()
        For Each obj As Object In GetEmployeeName.GetEmployee.Values
            cboName.Items.Add(obj)
        Next
    End Sub

    Private Sub cboName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboName.SelectedIndexChanged
        Dim group As EmployeeInfo = CType(cboName.SelectedItem, EmployeeInfo)
        empId = group.EmployeeId
    End Sub

    Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click
        SaveUpdate()
    End Sub
    Private Sub SaveUpdate()
        Try
            'Passing values from object in class to TextBox
            With edu
                .EducationId = eduId
                .EmployeeId = empId
                .Duration = txtDuration.Text
                .Major = txtMajor.Text
                .Certificate = txtCertificate.Text
                .SchoolName = txtUniversity.Text
                .Country = cboCountry.Text
                .Note = txtNote.Text
                .EducationGroup = txtEducationGroup.Text
                .Gpa = CboGPA.Text
                .GraduateYear = txtGrateuateYear.Text
                .Active = True

                If eduId = Nothing Then
                    'Insert Education

                    Educations.AddEducation(edu)
                Else
                    'Update Education

                    Educations.UpdateEducation(edu)

                End If

            End With
            AddEducationToDataGridView()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Close()
    End Sub

    Public Sub AddEducationToDataGridView()
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If
            'Passing vlues DataGridView in form Education
            With FormEducation.dgvEducation.CurrentRow
                .Cells("EMPLOYEE_NAME").Value = cboName.Text
                .Cells("DURATION").Value = edu.Duration
                .Cells("MAJOR").Value = edu.Major
                .Cells("CERTIFICATE").Value = edu.Certificate
                .Cells("SCHOOL_NAME").Value = edu.SchoolName
                .Cells("COUNTRY").Value = edu.Country
                .Cells("NOTE").Value = edu.Note
                .Cells("EDUCATION_GROUP").Value = edu.EducationGroup
                .Cells("GPA").Value = edu.Gpa
                .Cells("GRADUATE_YEAR").Value = edu.GraduateYear
                If eduId = Nothing Then

                    .Cells("EDUCATOIN_ID").Value = edu.EducationId
                    RefreshData.AscendingRefresh(FormEducation.dgvEducation, 0)

                    FormEducation.dgvEducation.CurrentCell = FormEducation.dgvEducation.Rows(0).Cells(1)
                Else

                    FormEducation.dgvEducation.CurrentCell = FormEducation.dgvEducation.Rows(0).Cells(1)

                    Exit Sub
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
 
    Private Sub FormEducationAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class